package com.study.english.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.english.constant.SecurityConstants;
import com.study.english.context.SecurityContextHolder;
import com.study.english.domain.SysVideoType;
import com.study.english.pojo.vo.SysVideoVo;
import com.study.english.result.Result;
import com.study.english.service.ISysVideoService;
import com.study.english.service.ISysVideoTypeService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

import com.study.english.domain.SysVideo;

import org.springframework.web.bind.annotation.*;

/**
 * 学习视频
 *
 * @author wf
 * @description 针对表【sys_video(学习视频)】的数据库操作Controller
 * @createDate 2024-03-11 15:56:35
 */
@RestController
@RequestMapping("sysVideo")
public class SysVideoController {

    @Autowired
    private ISysVideoService sysVideoService;

    @Autowired
    private ISysVideoTypeService sysVideoTypeService;

    /**
     * 新增:学习视频
     */
    @PostMapping("addSysVideo")
    public Result addSysVideo(@RequestBody SysVideoVo sysVideoVo) {
        sysVideoService.addSysVideo(sysVideoVo);
        return Result.success();

    }

    /**
     * 新增:批量学习视频
     */
    @PostMapping("addBatchSysVideo")
    public Result addBatchSysVideo(@RequestBody SysVideoVo sysVideoVo) {
        sysVideoService.addBatchSysVideo(sysVideoVo);
        return Result.success();

    }

    /**
     * 分页查询:学习视频
     */
    @PostMapping("getSysVideoPageList")
    public Result getSysVideoPageList(@RequestBody SysVideoVo sysVideoVo) {

        IPage<SysVideoVo> page = new Page<>(sysVideoVo.getPageNum(), sysVideoVo.getPageSize());
        IPage<SysVideoVo> list = sysVideoService.getSysVideoPageList(page, sysVideoVo);
        return Result.success(list);
    }


    /**
     * 不分页查询:学习视频
     */

    @PostMapping("getSysVideoList")
    public Result getSysVideoList(@RequestBody SysVideoVo sysVideoVo) {
        sysVideoVo.setUserId(SecurityContextHolder.getUserId());

        List<SysVideoVo> sysVideoVoList = sysVideoService.getSysVideoList(sysVideoVo);
        return Result.success(sysVideoVoList);
    }


    /**
     * 不分页查询:查询正在学习视频
     */

    @PostMapping("getSysVideoCurrStudyList")
    public Result getSysVideoCurrStudyList(@RequestBody SysVideoVo sysVideoVo) {
        sysVideoVo.setUserId(SecurityContextHolder.getUserId());
        sysVideoVo.setStatus(1);
        sysVideoVo.setVideoTypeStatus(1);
        sysVideoVo.setSysVideoType(0);
        sysVideoVo.setSortField("sortNum");
        sysVideoVo.setSortRule("asc");
        List<SysVideoVo> sysVideoVoList = sysVideoService.getSysVideoCurrStudyList(sysVideoVo);
        return Result.success(sysVideoVoList);
    }

    /**
     * 根据id查询详情:学习视频
     */

    @GetMapping("getSysVideoById")
    public Result getSysVideoById(@RequestParam String id) {
        SysVideo sysVideo = sysVideoService.getSysVideoById(id);
        return Result.success(sysVideo);
    }

    /**
     * 根据id删除:学习视频
     */
    @GetMapping("delSysVideo")
    public Result delSysVideo(@RequestParam String id) {
        sysVideoService.delSysVideo(id);
        return Result.success();
    }


    /**
     * 修改:学习视频
     */
    @PostMapping("updateSysVideo")
    public Result updateSysVideo(@RequestBody SysVideoVo sysVideoVo) {
        sysVideoService.updateSysVideo(sysVideoVo);
        return Result.success();
    }

    /**
     * 更新缩略图
     */
    @GetMapping("updateThumbnailImage")
    public Result updateThumbnailImage(@RequestParam Long id) {
        String ossHead = SecurityConstants.OSS_HTTP_HEAD + SecurityConstants.OSS_BUCKET + "." + SecurityConstants.OSS_ENDPOINT;
        SysVideo sysVideo = sysVideoService.getById(id);
        String url = sysVideoService.uploadThumbnailImage(sysVideo);
        try {

            String[] fileUrlArr = sysVideo.getThumbnailUrl().split("/");
            String[] fileUrlNewArr = Arrays.copyOfRange(fileUrlArr, 2, fileUrlArr.length);
            String delObjectName = "";
            for (String s : fileUrlNewArr) {
                delObjectName = delObjectName + (delObjectName == "" ? s : "/" + s);
            }
        } catch (Exception e) {
            return Result.failed(e.getMessage());
        }
        sysVideoService.update(new UpdateWrapper<SysVideo>().eq("id", id).set("thumbnail_url", "/" + url));
        return Result.success();

    }

    //手动生成任务
    @GetMapping("manualVideoTask")
    public Result manualVideoTask(@RequestParam Long id) {
        SysVideoType sysVideoType = sysVideoTypeService.getById(id);
        List<SysVideo> sysVideoList = sysVideoService.list(new QueryWrapper<SysVideo>().orderByAsc("sort_num").eq("status", 0).eq("video_type_id", id).last("limit " + sysVideoType.getStudyNum()));
        sysVideoService.update(new UpdateWrapper<SysVideo>().set("status", 2).eq("status", 1).eq("video_type_id", sysVideoType.getId()));
        for (SysVideo sysVideo : sysVideoList) {
            sysVideo.setStatus(1);
        }
        sysVideoService.updateBatchById(sysVideoList);
        return Result.success();
    }
}
